/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.domain.CustomUserDetails;
import cc.altius.model.*;
import cc.altius.domain.User;
import cc.altius.service.CenterService;
import cc.altius.service.CustomerService;
import cc.altius.service.DoctorService;
import cc.altius.service.HospitalService;
import cc.altius.service.OfficeLocationService;
import cc.altius.service.SpecialistService;
import cc.altius.service.TicketService;
import cc.altius.service.UserService;
import cc.altius.utils.DateUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.jasypt.util.numeric.BasicIntegerNumberEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author sagar
 */
@Controller
public class CustomerEnquiryController {
    
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CenterService centerService;
    @Autowired
    private OfficeLocationService officeLocationService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private SpecialistService specialistService;
    @Autowired
    private BasicIntegerNumberEncryptor basicIntegerNumberEncryptor;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private UserService userService;
    
    @ModelAttribute("illnessList")
    public List<Illness> populateIllnessList() {
        return customerService.getIllnessList();
    }
    
    @ModelAttribute("centerList")
    public List<HealthSpringCenter> populateCenterList() {
        return centerService.getCenterList();
    }
    
    @ModelAttribute("paymentTypeList")
    List<PaymentType> getPaymentTypeList() {
        return this.customerService.getPaymentTypeList();
    }
    
    @ModelAttribute("relationList")
    List<Relation> getRelationList() {
        return this.customerService.getRelationList();
    }
    
    @ModelAttribute("callTypeList")
    List<CallType> getCallTypeList() {
        return this.customerService.getCallTypeList();
    }
    
    @ModelAttribute("dispositionList")
    List<Disposition> getDispositionList() {
        List<Disposition> list = this.customerService.getDispositionList();
        return list;
    }
    
    @ModelAttribute("officeList")
    public List<OfficeLocation> getOfficeLocationsList() {
        return this.officeLocationService.getOfficeLocationsList();
    }
    
    @ModelAttribute("doctorList")
    public List<Doctor> populateDoctorList() {
        return doctorService.getDoctorList();
    }
    
    @RequestMapping(value = "/customer/addCustomer.htm", method = RequestMethod.GET)
    public String showAddCustomerPage(@ModelAttribute("customer") Customer customer) {
        return "/customer/addCustomer";
    }
    
    @RequestMapping(value = "/customer/addCustomer.htm", method = RequestMethod.POST)
    public String submitPage(@ModelAttribute("customer") Customer customer, Errors errors,
            @RequestParam(value = "dateOfBirth", defaultValue = "") String dateOfBirth,
            @RequestParam(value = "dateOfJoining", defaultValue = "") String dateOfJoining,
            @RequestParam(value = "area", defaultValue = "") String area,
            //@RequestParam(value = "recordingId", defaultValue = "") String recordingId,
            @RequestParam(value = "masterZone", defaultValue = "") String masterZone,
            SessionStatus status, HttpServletRequest request,
            ModelMap modelMap) throws ServletRequestBindingException, ParseException {

        //String followupDate = ServletRequestUtils.getStringParameter(request, "followupDate", "0");
        String selectedIllnessIds = ServletRequestUtils.getStringParameter(request, "selectedIllnessIds", "0");
        System.out.println("selectedIllnessIds :" + selectedIllnessIds);
        //hospitalName.trim();
        //String appSpecialistName = ServletRequestUtils.getStringParameter(request, "appSpecialistId");
        //appSpecialistName.trim();
        Date date = null;
        Date dob = null;
        Date doj = null;
//        if (!"".equals(followupDate)) {
//            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
//            date = (Date) dateFormat.parse(followupDate);
//            customer.setFollowupDate(date);
//        }

        if (!"".equals(dateOfBirth)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dob = (Date) dateFormat.parse(dateOfBirth);
            customer.setDateOfBirth(dob);
        }
        
        if (!"".equals(dateOfJoining)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            doj = (Date) dateFormat.parse(dateOfJoining);
            customer.setDateOfJoining(doj);
        }
        if ("N/A".equals(area) || "N/A".equals(masterZone) || "".equals(area) || "".equals(masterZone)) {
            customer.setZipCodeArea(null);
        }

//        int hospitalId = 0;
//        int appSpecialistId = 0;
//        if (!"".equals(hospitalName)) {
//            String temp1[] = hospitalName.split("~");
//            hospitalId = Integer.parseInt(temp1[1].trim());
//        }
//        if (!"".equals(appSpecialistName)) {
//            String temp2[] = appSpecialistName.split("~");
//            appSpecialistId = Integer.parseInt(temp2[1].trim());
//        }
        int customerId = this.customerService.addCustomer(customer);
        //int customerId = this.customerService.addCustomer(customer, recordingId, hospitalId, appSpecialistId);

        int illnessIds = this.customerService.addIllness(customerId, selectedIllnessIds);
        
        if (customerId == 0) {
            errors.rejectValue("customer", "msg.customerError");
            return "/customer/addCustomer";
        } else {
            status.setComplete();
            return "redirect:/customer/addCustomer.htm?msg=msg.customerAddedSuccessfully";
        }
    }
    
    @RequestMapping(value = "/customer/updateCustomer.htm", method = RequestMethod.GET)
    public String showCustomerDetails(@RequestParam(value = "customerId") int customerId, ModelMap modelMap) {
        
        Customer customer = this.customerService.getCustomerDetails(customerId);
        Date follwUpDate = customer.getFollowupDate();
        if (follwUpDate != null) {
            String s1 = DateUtils.formatDate(follwUpDate, "yyyy-MM-dd hh:mm");
            modelMap.addAttribute("follwUpDate", s1);
        }
        
        modelMap.addAttribute("customer", customer);
        List<TransCustomer> customerlistTrans = this.customerService.getCustomerTransDetails(customerId);
        modelMap.addAttribute("customerTrans", customerlistTrans);
        List<Map<String, Object>> illnessListRight = this.customerService.getIllnessListByCustomerId(customerId);
        List<Map<String, Object>> illnessListLeft = this.customerService.getIllnessListForLeftTable(customerId);
        
        modelMap.addAttribute("illnessListRight", illnessListRight);
        modelMap.addAttribute("illnessListLeft", illnessListLeft);
        return "customer/updateCustomer";
    }
    
    @RequestMapping(value = "/customer/updateCustomer.htm", method = RequestMethod.POST, params = "_submitAndSendSms")
    public String updateCustomerAndSendSMS(
            @RequestParam(value = "transFollowupDate") String transFollowupDate,
            @RequestParam(value = "dateOfBirth1") String dateOfBirth1,
            @RequestParam(value = "dateOfJoining1") String dateOfJoining1,
            @RequestParam(value = "transCheifComplaint") String transCheifComplaint,
            @RequestParam(value = "transNotes") String transNotes,
            @RequestParam(value = "area", defaultValue = "") String area,
            @RequestParam(value = "recordingId", defaultValue = "") String recordingId,
            @RequestParam(value = "masterZone", defaultValue = "") String masterZone,
            @ModelAttribute("customer") Customer customer, HttpServletRequest request) throws ParseException, ServletRequestBindingException {
        
        String hospitalName = ServletRequestUtils.getStringParameter(request, "hospitalId");
        hospitalName.trim();
        String appSpecialistName = ServletRequestUtils.getStringParameter(request, "appSpecialistId");
        appSpecialistName.trim();
        Date date = null;
        if (!"".equals(transFollowupDate)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            date = (Date) dateFormat.parse(transFollowupDate);
            customer.setFollowupDate(date);
        }
        
        if (!"".equals(dateOfBirth1)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dob = (Date) dateFormat.parse(dateOfBirth1);
            customer.setDateOfBirth(dob);
        }
        
        if (!"".equals(dateOfJoining1)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date doj = (Date) dateFormat.parse(dateOfJoining1);
            customer.setDateOfJoining(doj);
        }
        
        String selectedIllnessIds = ServletRequestUtils.getStringParameter(request, "selectedIllnessIds", "0");
        int callTypeId = customer.getCallType().getCallTypeId();
        int dispositionId = customer.getDisposition().getDispositionId();
        int customerId = customer.getCustomerId();
        int hospitalId = 0;
        int appSpecialistId = 0;
        if (!"".equals(hospitalName)) {
            String temp1[] = hospitalName.split("~");
            hospitalId = Integer.parseInt(temp1[1].trim());
        }
        if (!"".equals(appSpecialistName)) {
            String temp2[] = appSpecialistName.split("~");
            appSpecialistId = Integer.parseInt(temp2[1].trim());
        }
        try {
            this.customerService.updateCustomer(customer, date, transNotes, transCheifComplaint, callTypeId, dispositionId, area, recordingId, hospitalId, appSpecialistId);
            this.customerService.updateIllness(customerId, selectedIllnessIds);
            return "redirect:../sms/updateCustomerForSms.htm?msg=Customer updated successfully...&customerId=" + customer.getCustomerId();
        } catch (Exception e) {
        }
        return "redirect:../sms/updateCustomerForSms.htm?error=Error while trying to update the customer..&customerId=" + customer.getCustomerId();
    }
    
    @RequestMapping(value = "/customer/updateCustomer.htm", method = RequestMethod.POST, params = "_submit")
    public String updateCustomerDetails(
            @RequestParam(value = "transFollowupDate") String transFollowupDate,
            @RequestParam(value = "dateOfBirth1") String dateOfBirth1,
            @RequestParam(value = "dateOfJoining1") String dateOfJoining1,
            @RequestParam(value = "transCheifComplaint") String transCheifComplaint,
            @RequestParam(value = "transNotes") String transNotes,
            @RequestParam(value = "area", defaultValue = "") String area,
            @RequestParam(value = "recordingId", defaultValue = "") String recordingId,
            @RequestParam(value = "masterZone", defaultValue = "") String masterZone,
            @ModelAttribute("customer") Customer customer, HttpServletRequest request) throws ParseException, ServletRequestBindingException {
        
        String hospitalName = ServletRequestUtils.getStringParameter(request, "hospitalId");
        hospitalName.trim();
        
        String appSpecialistName = ServletRequestUtils.getStringParameter(request, "appSpecialistId");
        appSpecialistName.trim();
        Date date = null;
        if (!"".equals(transFollowupDate)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            date = (Date) dateFormat.parse(transFollowupDate);
            customer.setFollowupDate(date);
        }
        
        if (!"".equals(dateOfBirth1)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dob = (Date) dateFormat.parse(dateOfBirth1);
            customer.setDateOfBirth(dob);
        }
        if (!"".equals(dateOfJoining1)) {
            
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date doj = (Date) dateFormat.parse(dateOfJoining1);
            customer.setDateOfJoining(doj);
        }
        String selectedIllnessIds = ServletRequestUtils.getStringParameter(request, "selectedIllnessIds", "0");
        int callTypeId = customer.getCallType().getCallTypeId();
        int dispositionId = customer.getDisposition().getDispositionId();
        int customerId = customer.getCustomerId();
        int hospitalId = 0;
        int appSpecialistId = 0;
        if (!"".equals(hospitalName)) {
            String temp1[] = hospitalName.split("~");
            hospitalId = Integer.parseInt(temp1[1].trim());
        }
        if (!"".equals(appSpecialistName)) {
            String temp2[] = appSpecialistName.split("~");
            appSpecialistId = Integer.parseInt(temp2[1].trim());
        }
        try {
            this.customerService.updateCustomer(customer, date, transNotes, transCheifComplaint, callTypeId, dispositionId, area, recordingId, hospitalId, appSpecialistId);
            
            this.customerService.updateIllness(customerId, selectedIllnessIds);
            
            return "redirect:followupList.htm?msg=Customer Updated successfully....";
        } catch (Exception e) {
            return "redirect:followupList.htm?error= Error while tying to update the customer....";
        }
        
    }
    
    @RequestMapping(value = "/customer/updateCustomer.htm", method = RequestMethod.POST, params = "_cancel")
    public String updateCustomerDetails(
            SessionStatus sessionStatus) {
        sessionStatus.setComplete();
        return "redirect:followupList.htm";
    }
    
    @RequestMapping(value = "getZipCodeArea.htm", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxGetForZipCodeArea(@RequestParam("zipCode1") String zipCode1) {
        ZipCodeArea zipCodeAreas = this.hospitalService.getZipCodeByZipCode(zipCode1);
        Gson gson = new Gson();
        String json = gson.toJson(zipCodeAreas);
        return json.toString();
    }
    
    @RequestMapping(value = "ajaxCallForHospitalList.htm", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxCustomerPopulate(HttpServletRequest request) {//ajaxCallForHospitalList
        System.out.println("inside doAjaxCustomerPopulate");
        String str = request.getParameter("term");
        String json;
        List<Hospital> hospitalList = this.hospitalService.hospitalListForAutoComplete(str);
        System.out.println("list" + hospitalList.size());
        Gson gson = new Gson();
        List list = new ArrayList();
        for (Hospital h : hospitalList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", h.getHospitalId());
            map.put("label", h.getHospitalName() + " ~ " + h.getHospitalId());
            map.put("value", h.getHospitalName() + " ~ " + h.getHospitalId());
            list.add(map);
        }
        Type typeList = new TypeToken<List>() {
        }.getType();
        json = gson.toJson(list, typeList);
        System.out.println("json :" + json);
        return json;
    }
    
    @RequestMapping(value = "ajaxCallForAppSpecialist.htm", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxAppSpecialistPopulate(HttpServletRequest request) {
        System.out.println("inside doAjaxAppSpecialistPopulate");
        String str = request.getParameter("term");
        System.out.println("term :" + str);
        String json;
        List<AppSpecialist> appSpecialists = this.specialistService.AppSpecialistListForAutoComplete(str);
        System.out.println("list" + appSpecialists.size());
        Gson gson = new Gson();
        List list = new ArrayList();
        for (AppSpecialist as : appSpecialists) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", as.getAppSpecialistId());
            map.put("label", as.getName() + " ~ " + as.getAppSpecialistId());
            map.put("value", as.getName() + " ~ " + as.getAppSpecialistId());
            list.add(map);
        }
        Type typeList = new TypeToken<List>() {
        }.getType();
        json = gson.toJson(list, typeList);
        System.out.println("json :" + json);
        return json;
    }
    
    @RequestMapping(value = "ajaxCallForDispositionList.htm", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxDispositionPopulate(@RequestParam(value = "callTypeId1") int callTypeId) {
        System.out.println("inside ajax call.........");
        String json;
        List<Disposition> dispositionList = this.customerService.getDispositionList(callTypeId);
        Gson gson = new Gson();
        List list = new ArrayList();
        for (Disposition d : dispositionList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", d.getDispositionId());
            map.put("label", d.getDispositionName());
            map.put("value", d.getDispositionId());
            list.add(map);
        }
        Type typeList = new TypeToken<List>() {
        }.getType();
        json = gson.toJson(list, typeList);
        System.out.println("json :" + json);
        return json;
    }

    //FOR ONLINE REGISTRATION
    @RequestMapping(value = "/customer/onlineRegistrationForm.htm", method = RequestMethod.GET)
    public String showOnlineRegistrationForm(@RequestParam(value = "customerId") String customerId, ModelMap modelMap) {
        try {
            BigInteger customerId1 = this.basicIntegerNumberEncryptor.decrypt(new BigInteger(customerId));
            int customerId2 = customerId1.intValue();
            System.out.println("customerId2 :" + customerId2);
            Customer customer = this.customerService.getCustomerDetails(customerId2);
            customer.setCustomerId(customerId2);
            System.out.println("customer id using get :" + customer);
            List<Map<String, Object>> illnessListRight = this.customerService.getIllnessListByCustomerId(customerId2);
            System.out.println("illnessListRight :" + illnessListRight);
            List<Map<String, Object>> illnessListLeft = this.customerService.getIllnessListForLeftTable(customerId2);
            System.out.println("illnessListLeft :" + illnessListLeft);
            //modelMap.addAttribute("customerId2", customerId2);
            modelMap.addAttribute("customer", customer);
            modelMap.addAttribute("illnessListRight", illnessListRight);
            modelMap.addAttribute("illnessListLeft", illnessListLeft);
            return "/customer/onlineRegistrationForm";
        } catch (Exception e) {
            return "/customer/accessDenied";
        }
    }
    
    @RequestMapping(value = "/customer/onlineRegistrationForm.htm", method = RequestMethod.POST)
    public String submitOnlineRegistrationForm(
            @RequestParam(value = "dateOfBirth1") String dateOfBirth1,
            @RequestParam(value = "dateOfJoining1") String dateOfJoining1,
            @RequestParam(value = "area", defaultValue = "") String area,
            @RequestParam(value = "masterZone", defaultValue = "") String masterZone,
            @ModelAttribute("customer") Customer customer,
            HttpServletRequest request) throws ParseException, ServletRequestBindingException {
        System.out.println("customer id in post :" + customer);
        
        if (!"".equals(dateOfBirth1)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dob = (Date) dateFormat.parse(dateOfBirth1);
            customer.setDateOfBirth(dob);
        }
        
        if (!"".equals(dateOfJoining1)) {
            
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date doj = (Date) dateFormat.parse(dateOfJoining1);
            customer.setDateOfJoining(doj);
        }
        
        String selectedIllnessIds = ServletRequestUtils.getStringParameter(request, "selectedIllnessIds", "0");
        int customerId = customer.getCustomerId();
        System.out.println(" customerId :::" + customerId);
        try {
            this.customerService.updateCustomerForOnlineRegistration(customer);
            
            this.customerService.updateIllness(customerId, selectedIllnessIds);
            
            return "redirect:success.htm?msg=msg.registrationSuccessful";
        } catch (Exception e) {
            System.out.println(e.toString());
            return "redirect:failure.htm?error=msg.registrationFailed";
        }
        
        
    }
    
    @RequestMapping(value = "/customer/editCustomer.htm", method = RequestMethod.GET)
    public String showEditCustomerGetPage(@RequestParam(value = "customerId") int customerId, ModelMap modelMap) {
        Customer customer = this.customerService.getCustomerDetails(customerId);
        modelMap.addAttribute("customer", customer);
        List<Map<String, Object>> illnessListRight = this.customerService.getIllnessListByCustomerId(customerId);
        List<Map<String, Object>> illnessListLeft = this.customerService.getIllnessListForLeftTable(customerId);
        List<Ticket> openTickets = this.ticketService.getOpenTicketList(customerId);
        System.out.println("customer object :" + customer);
        List<Ticket> closedTickets = this.ticketService.getClosedTicketList(customerId);
        modelMap.addAttribute("closedTickets", closedTickets);
        modelMap.addAttribute("openTickets", openTickets);
        modelMap.addAttribute("illnessListRight", illnessListRight);
        modelMap.addAttribute("illnessListLeft", illnessListLeft);
        return "customer/editCustomer";
    }
    
    @RequestMapping(value = "/customer/editCustomer.htm", method = RequestMethod.POST)
    public String showEditCustomerPostPage(
            @RequestParam(value = "dateOfBirth1") String dateOfBirth1,
            @RequestParam(value = "dateOfJoining1") String dateOfJoining1,
            @RequestParam(value = "area", defaultValue = "") String area,
            @RequestParam(value = "masterZone", defaultValue = "") String masterZone,
            @ModelAttribute("customer") Customer customer,
            HttpServletRequest request,
            ModelMap modelMap) throws ParseException, ServletRequestBindingException {
        int customerId = customer.getCustomerId();
        System.out.println("customer id in post :" + customer);
        if (!"".equals(dateOfBirth1)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dob = (Date) dateFormat.parse(dateOfBirth1);
            customer.setDateOfBirth(dob);
        }
        if (!"".equals(dateOfJoining1)) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date doj = (Date) dateFormat.parse(dateOfJoining1);
            customer.setDateOfJoining(doj);
        }
        String selectedIllnessIds = ServletRequestUtils.getStringParameter(request, "selectedIllnessIds", "0");
        //int customerId = customer.getCustomerId();
        System.out.println(" customerId :::" + customerId);
        try {
            this.customerService.updateCustomerForOnlineRegistration(customer);
            
            this.customerService.updateIllness(customerId, selectedIllnessIds);
            
            return "redirect:../home/index.htm?customerId="+customerId+"&msg=Customer Updated successfully....";
        } catch (Exception e) {
            System.out.println(e.toString());
            return "redirect:../home/index.htm?error= Error while tying to update the customer....";
        }
    }

    // FOR TICKET
    @RequestMapping(value = "/ticket/addTicket.htm", method = RequestMethod.GET)
    public String showAddTicketGetPage(@RequestParam("customerId") int customerId, ModelMap modelMap) {
        Ticket ticket = new Ticket();
        ticket.setCustomer(new Customer(customerId));
        List<User> activeUserList = this.userService.getActiveUserListForTicket();
        modelMap.addAttribute("activeUserList", activeUserList);
        modelMap.addAttribute("ticket", ticket);
        return "ticket/addTicket";
    }
    
    @RequestMapping(value = "/ticket/addTicket.htm", method = RequestMethod.POST)
    public String showAddTicketPostPage(@ModelAttribute("ticket") Ticket ticket,
            @RequestParam(value = "recordingId", defaultValue = "") String recordingId,
            HttpServletRequest request,
            ModelMap modelMap) throws ServletRequestBindingException, ParseException {
        String followupDate = ServletRequestUtils.getStringParameter(request, "followupDate1", "0");
        String hospitalName = ServletRequestUtils.getStringParameter(request, "hospitalId");
        hospitalName.trim();
        String appSpecialistName = ServletRequestUtils.getStringParameter(request, "appSpecialistId");
        appSpecialistName.trim();
        int callTypeId = ticket.getCallType().getCallTypeId();
        int dispositionId = ticket.getDisposition().getDispositionId();
        int customerId = ticket.getCustomer().getCustomerId();
        int hospitalId = 0;
        int appSpecialistId = 0;
        if (!"".equals(hospitalName)) {
            
            String temp1[] = hospitalName.split("~");
            hospitalId = Integer.parseInt(temp1[1].trim());
            
        }
        if (!"".equals(appSpecialistName)) {
            String temp2[] = appSpecialistName.split("~");
            appSpecialistId = Integer.parseInt(temp2[1].trim());
            
        }
        ticket.setRecordingId(recordingId);
        try {
            int ticketId = this.ticketService.addTicket(ticket, followupDate, hospitalId, appSpecialistId);
            return "redirect:addTicketStatus.htm?msg=msg.ticketAddedSuccessfully";
        } catch (Exception e) {
            return "redirect:addTicketStatus.htm?error=msg.errorWhileAddingTicket";
        }
        //return "ticket/addTicket";
    }
    
    @RequestMapping(value = "/ticket/editTicket.htm", method = RequestMethod.GET)
    public String showEditTicketGetPage(@RequestParam("ticketId") int ticketId,
            @RequestParam("customerId") int customerId,
            ModelMap modelMap) {
        //Ticket ticket = this.ticketService.getTicketByTicketId(ticketId);
        Ticket ticket = new Ticket();
        ticket.setTicketId(ticketId);
        ticket.setCustomer(new Customer(customerId));
        List<TransTicket> transTickets = this.ticketService.getTicketTransDetails(ticketId);
        List<User> activeUserList = this.userService.getActiveUserListForTicket();
        //modelMap.addAttribute("customerId", customerId);
        modelMap.addAttribute("activeUserList", activeUserList);
        modelMap.addAttribute("ticket", ticket);
        modelMap.addAttribute("transTickets", transTickets);
        return "ticket/editTicket";
    }
    
    @RequestMapping(value = "/ticket/editTicket.htm", method = RequestMethod.POST)
    public String showEditTicketPostPage(@ModelAttribute("ticket") Ticket ticket,
            @RequestParam(value = "recordingId", defaultValue = "") String recordingId,
            HttpServletRequest request,
            ModelMap modelMap) throws ServletRequestBindingException {
        
        String followupDate = ServletRequestUtils.getStringParameter(request, "followupDate1", "0");
        String hospitalName = ServletRequestUtils.getStringParameter(request, "hospitalId");
        hospitalName.trim();
        String appSpecialistName = ServletRequestUtils.getStringParameter(request, "appSpecialistId");
        appSpecialistName.trim();
        
        int customerId = ticket.getCustomer().getCustomerId();
        int hospitalId = 0;
        int appSpecialistId = 0;
        if (!"".equals(hospitalName)) {
            String temp1[] = hospitalName.split("~");
            hospitalId = Integer.parseInt(temp1[1].trim());
        }
        if (!"".equals(appSpecialistName)) {
            String temp2[] = appSpecialistName.split("~");
            appSpecialistId = Integer.parseInt(temp2[1].trim());
        }
        ticket.setRecordingId(recordingId);
        try {
            this.ticketService.updateTicket(ticket, followupDate, hospitalId, appSpecialistId);
            return "redirect:editTicketStatus.htm?msg=msg.ticketUpdateSuccessful";
        } catch (Exception e) {
            return "redirect:editTicketStatus.htm?error=msg.ticketUpdateFailed";
        }
    }
    
    @RequestMapping(value = "/customer/success.htm", method = RequestMethod.GET)
    public String showSuccessPage() {
        return "customer/success";
    }
    
    @RequestMapping(value = "/customer/failure.htm", method = RequestMethod.GET)
    public String showFailurePage() {
        return "customer/failure";
    }
}
